package base.thread_JUC2.chapter04.demo01;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 执行任务的执行器，任务交由执行器执行
 */
public class Server {
    private ThreadPoolExecutor executor;
    public Server(){
        executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    }

    public void executeTask(Task task){
        System.out.println("到达一个新任务");
        executor.execute(task);
        System.out.printf("Server：执行器线程池大小 %d\n", executor.getPoolSize());
        System.out.printf("Server：执行器中正在执行任务的线程数 %d\n", executor.getActiveCount());
        System.out.printf("Server：已经完成的任务数 %d\n", executor.getCompletedTaskCount());
    }

    public void endServer(){
        executor.shutdown();
    }
}
